<?php
namespace app\admin\controller;
use think\Controller;

class Category extends Controller
{
    private $obj;
    public function _initialize() {
        $this->obj = model("Category");
    }

    public function index()
    {
        $parentId = input('param.parent_id', 0, 'intval');
        // dump($parentId);
        $categorys = $this->obj->getFirstCategorys($parentId);
        return $this->fetch('',[
                'categorys' => $categorys,
            ]);
    }

    public function add() {
        $categorys = $this->obj->getNormalFirstCategory();//获取一级分类
    	return $this->fetch('',[
                'categorys' => $categorys,
            ]);
    }

    public function save() {
    	//print_r($_POST);
    	//print_r(input('post.'));
    	// print_r(request()->post());

        /**
        * 做下严格判定
        */
        if(!request()->isPost()) {
            $this->error('请求失败');
        }

    	$data = input('post.');
    	// $data['status'] = 10;
    	$validate = validate('Category');
    	if(!$validate->scene('add')->check($data)) {
    		$this->error($validate->getError());
    	}
        if(!empty($data['id'])) {
            return $this->update($data);
        }

    	// 把$data 提交给model层
        $res = $this->obj->add($data);
        if($res) {
            $this->success('新增成功');
        }else {
            $this->error('新增失败');
        }
    }

    /**
    *编辑页面
    */
    public function edit($id = 0) {
        // echo $id;
        if(intval($id) < 1) {
            $this->error('参数不合法');
        }
        $category = $this->obj->get($id);//dump($category);exit;
        $categorys = $this->obj->getNormalFirstCategory();//获取一级分类
        return $this->fetch('',[
                'categorys' => $categorys,
                'category' => $category,
            ]);
    }

    public function update($data) {
        $res = $this->obj->save($data, ['id' => intval($data['id'])]);//save第二个参数是条件.
        if($res) {
            $this->success('更新成功');
        }else {
            $this->error('更新失败');
        }
    }

    //排序逻辑
    public function listorder($id, $listorder) {
        // echo $id."<br />";
        // echo $listorder."<br />";

        $res = $this->obj->save(['listorder'=>$listorder], ['id'=>$id]);
        if($res) {
            $this->result($_SERVER['HTTP_REFERER'], 1, '更新成功');
        }else {
            $this->result($_SERVER['HTTP_REFERER'], 0, '更新失败');
        }
    }

    //修改状态
    public function status() {
        //print_r(input('param.'));
        $data = input('param.');
        dump($data);exit;
        $validate = validate('Category');
        if(!$validate->scene('status')->check($data)) {
            $this->error($validate->getError());
        }

        $res = $this->obj->save(['status'=>$data['status']],['id'=>$data['id']]);
        if($res) {
            $this->success('状态更新成功');
        }else {
            $this->error('状态更新失败');
        }
    }
}
